package com.hlab.fabrevealmenu.view;

import com.hlab.fabrevealmenu.utils.DensityUtils;

import ohos.agp.components.TableLayoutManager;
import ohos.app.Context;

/**
 * DynamicGridLayoutManager
 *
 * @since 2021-05-14
 */
public class DynamicGridLayoutManager extends TableLayoutManager {
    private int minItemWidth;
    private int totalItems;
    private Context mContext;

    DynamicGridLayoutManager(Context context, int minItemWidth, int totalItems) {
        super();
        mContext = context;
        this.minItemWidth = minItemWidth;
        this.totalItems = totalItems;
        updateSpanCount();
    }

    private void updateSpanCount() {
        int spanCount = 1;
        if (minItemWidth != 0) {
            spanCount = Math.min(DensityUtils.getDisplayWidth(mContext) / minItemWidth, totalItems);
            if (spanCount < 1) {
                spanCount = 1;
            }
        }
        setColumnCount(spanCount);
    }
}